package com.nulldev.util.image2.textobf;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.IOException;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import com.nulldev.util.image.ImageUtil;
import com.nulldev.util.image2.textobf.TextObf.ObfuscationMethod;
import com.nulldev.util.io.FileUtil;
import com.nulldev.util.java.CloneUtils;
import com.nulldev.util.java.JVMUtil;
import com.nulldev.util.java.swing.SwingUtil;
import com.nulldev.util.java.swing.canvas.SmoothCanvas;
import com.nulldev.util.java.swing.font.FontManager;
import com.nulldev.util.java.utils.color.ColorUtil;
import com.nulldev.util.math.MathUtil;

import javax.imageio.ImageIO;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JLabel;

public class TextObfDemo extends JFrame {

	private JPanel contentPane;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					FontManager.setFont(FontManager.getFont("MADE Evolve Sans"), 14);
					TextObfDemo frame = new TextObfDemo("4chins made me do this.\nIt's pretty good though.\nI can enjoy formatting while shitposting.\n>implying this isn't java");
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public TextObfDemo(final String text) {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 786, 469);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		
		final BufferedImage obf0 = TextObf.obfuscate(text, ObfuscationMethod.PixelObfuscation, true);
		final BufferedImage obf = TextObf.obfuscate(text, ObfuscationMethod.Noise, true);
		final BufferedImage obf1 = TextObf.obfuscate(text, ObfuscationMethod.PixelsAndNoise, true);
		final BufferedImage obf2 = TextObf.obfuscate(text, ObfuscationMethod.DualPixelObfuscation, true);
		final SmoothCanvas panel = new SmoothCanvas() {
			
			@Override
			public void drawCanvas(Graphics2D g2d) {
				g2d.drawImage(obf0, 0, 0, null);
			}
		};
		final SmoothCanvas panel_1 = new SmoothCanvas() {
			
			@Override
			public void drawCanvas(Graphics2D g2d) {
				g2d.drawImage(obf, 0, 0, null);
			}
		};
		panel_1.setAAEnabled(true);
		try {
			ImageIO.write(obf2, "png", FileUtil.safeCreate("[redacted]"));
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		JLabel lblNoObufscation = new JLabel("Obfuscation via Pixels");
		
		JLabel lblObfuscation = new JLabel("Obfuscation via Noise");
		
		final SmoothCanvas panel_2 = new SmoothCanvas() {
			
			@Override
			public void drawCanvas(Graphics2D g2d) {
				g2d.drawImage(obf1, 0, 0, null);
			}
		};
		
		JLabel lblObufscationViaPixelsx = new JLabel("Obfuscation via Pixels and Noise");
		
		final SmoothCanvas panel_3 = new SmoothCanvas() {
			
			@Override
			public void drawCanvas(Graphics2D g2d) {
				g2d.drawImage(obf2, 0, 0, null);
			}
		};
		
		JLabel lblObufscationViaDual = new JLabel("Obfuscation via Dual Pixels");
		GroupLayout gl_contentPane = new GroupLayout(contentPane);
		gl_contentPane.setHorizontalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_contentPane.createSequentialGroup()
							.addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING)
								.addComponent(panel_2, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 342, Short.MAX_VALUE)
								.addComponent(panel, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 342, Short.MAX_VALUE)
								.addComponent(lblNoObufscation, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 342, Short.MAX_VALUE))
							.addPreferredGap(ComponentPlacement.UNRELATED))
						.addGroup(gl_contentPane.createSequentialGroup()
							.addComponent(lblObufscationViaPixelsx)
							.addGap(130)))
					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_contentPane.createSequentialGroup()
							.addComponent(lblObufscationViaDual)
							.addContainerGap(209, Short.MAX_VALUE))
						.addComponent(panel_3, GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
						.addComponent(panel_1, GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
						.addComponent(lblObfuscation, GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)))
		);
		gl_contentPane.setVerticalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING, false)
						.addComponent(panel_1, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
						.addComponent(panel, GroupLayout.DEFAULT_SIZE, 169, Short.MAX_VALUE))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblNoObufscation)
						.addComponent(lblObfuscation))
					.addGap(18)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING, false)
						.addComponent(panel_3, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
						.addComponent(panel_2, GroupLayout.DEFAULT_SIZE, 163, Short.MAX_VALUE))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblObufscationViaPixelsx)
						.addComponent(lblObufscationViaDual))
					.addContainerGap(27, Short.MAX_VALUE))
		);
		contentPane.setLayout(gl_contentPane);
	}
}
